package com.kye.express.business.jd.search_list.search;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.datasource.HelpDataSource;
import com.kye.model.storage.HelpHistorySearchDataSource;

import java.util.List;

import io.reactivex.MaybeObserver;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

/**
 * 京东兑换-搜索 Presenter
 * <p>
 * Created by SkyYu on 2018/1/28
 */
public interface JDGoodsSearchContract {

    interface JDGoodsSearchView extends IView {
        void onLabelClicked(String item);

        void updateQueryRecordSuccess(List<String> datas);

        void updateQueryHistoryFailed(Throwable e);
    }

    class JDGoodsSearchPresenter extends BasePresenter<JDGoodsSearchView> {

        HelpDataSource helpDataSource;
        HelpHistorySearchDataSource helpHistorySearchDataSource;

        JDGoodsSearchPresenter() {
            helpHistorySearchDataSource = new HelpHistorySearchDataSource();
            helpDataSource = new HelpDataSource();
        }

        boolean saveQueryData(String queryData) {
            return helpHistorySearchDataSource.setSearchData(queryData);
        }

        void saveQueryDataObservable(String queryData) {
            Observable.just(queryData)
                    .map(new Function<String, Boolean>() {
                        @Override
                        public Boolean apply(String s) throws Exception {
                            return saveQueryData(s);
                        }
                    })
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onNext(Boolean saveSuccess) {
                            if (saveSuccess) {
                                updateSearchHistoryData();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        void updateSearchHistoryData() {
            helpHistorySearchDataSource
                    .getQueryDataRx()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<String>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onNext(List<String> strings) {
                            getView().updateQueryRecordSuccess(strings);
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().updateQueryHistoryFailed(e);
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        void clearSearchHistoryData() {
            helpHistorySearchDataSource
                    .removeQueryDataRx()
                    .filter(new Predicate<Boolean>() {
                        @Override
                        public boolean test(Boolean success) throws Exception {
                            return success;
                        }
                    })
                    .map(new Function<Boolean, List<String>>() {
                        @Override
                        public List<String> apply(Boolean aBoolean) throws Exception {
                            return helpHistorySearchDataSource.getQueryData();
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new MaybeObserver<List<String>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(List<String> strings) {
                            getView().updateQueryRecordSuccess(strings);
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().updateQueryHistoryFailed(e);
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }
}